
async function render() {
  // 获取当前 URL 的查询字符串
  const queryString = window.location.search;

  // 去除开头的问号
  const searchParams = new URLSearchParams(queryString.slice(1));

  // 获取 theme_id 和 themes_name 参数
  const theme_id = searchParams.get('theme_id');
  const themes_name = searchParams.get('themes_name');

  document.querySelector("#lxsg").innerHTML = themes_name
  // 输出获取到的参数
  const res = await axios({ url: "/getPoems", params: { theme_id } })
  if (!res.code) {
    const str = res.msg.map(item => `<div data-id="${item.poem_id}" class="card bg-white rounded-lg shadow-md overflow-hidden">
    <img src="./asset/image2.png" 
         class="w-full aspect-ratio-[300/169] object-cover"
         alt="${item.title}">
    <div class="p-4 flex flex-col justify-between">
      <h3 class="font-bold text-lg mb-1">${item.title}</h3>
      <p class="text-gray-600">${item.author}</p>
      <p class="mt-2 text-gray-800">${item.content}</p>
    </div>
  </div>`).join("")
    document.querySelector("#totalpoems").innerHTML = str
  }
}
render()

//跳转评论区
const totalpoems = document.getElementById('totalpoems');
totalpoems.addEventListener('click', function (event) {
  let target = event.target;
  // 向上查找具有 data-id 属性的元素
  while (target && !target.dataset.id) {
    target = target.parentElement;
  }
  if (target && target.dataset.id) {
    const poemId = target.dataset.id;
    window.location.href = `../detail/index.html?poem_id=${poemId}`;
  }
});